package cn.wuxun.user.service;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.wuxun.common.pojo.Manager;
import cn.wuxun.common.utils.MD5Util;
import cn.wuxun.common.utils.MapperUtil;
import cn.wuxun.common.utils.PrefixKey;
import cn.wuxun.common.vo.EasyUIResult;
import cn.wuxun.user.mapper.ManagerMapper;
import redis.clients.jedis.Jedis;

@Service
public class ManagerService {
	@Autowired
	private ManagerMapper managerMapper;
	private ObjectMapper mapper = MapperUtil.MP;
	@Autowired
	private Jedis jedis;

	public String doLogin(Manager manager) {
		// 根据用户名和密码查询用户是否存在mysql中
		try {
			manager.setPwd(MD5Util.md5(manager.getPwd()));
			Manager exist = managerMapper.queryManagerByNameAndPassword(manager);
			if (exist == null) {// 登录失败,没有数据可以存储在redis
				// 返回一个""
				return "";
			} else {
				String ticket = PrefixKey.ADMIN_TICKET_KEY + System.currentTimeMillis() + manager.getMname();
				String managerJson;
				managerJson = mapper.writeValueAsString(exist);
				// 判断当前用户是否曾经有人登录过
				String existTicket = jedis.get(PrefixKey.ADMIN_LOGINED_CHECK_KEY + exist.getMid());
				// 顶替实现.不允许前一个登录的人ticket存在
				if (StringUtils.isNotEmpty(existTicket)) {
					jedis.del(existTicket);
				}
				// 定义当前客户端登录的信息 managerId:ticket
				jedis.setex(PrefixKey.ADMIN_LOGINED_CHECK_KEY + exist.getMid(), 60 * 30, ticket);
				// 用户登录超时30分钟
				jedis.setex(ticket, 60 * 30, managerJson);
				return ticket;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	public void logout(Manager manager) {
		jedis.del(manager.getMid());
	}

	public String queryManagerJson(String ticket) {

		// ShardedJedis jedis = pool.getResource();
		String managerJson = "";
		try {
			// 首先判断超时剩余时间
			Long leftTime = jedis.pttl(ticket);
			// 少于10分钟,延长5分钟
			if (leftTime < 1000 * 60 * 10l) {
				jedis.pexpire(ticket, leftTime + 1000 * 60 * 5);
			}
			managerJson = jedis.get(ticket);
			return managerJson;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
//修改密码
	public void modifyManagerPassword(Manager manager) {
		// TODO Auto-generated method stub

	}
//增加管理员
	public void insertManager(Manager manager) {
		manager.setMid(UUID.randomUUID().toString());
		manager.setPwd(MD5Util.md5(manager.getPwd()));
		managerMapper.insertManager(manager);
		
	}
//删除管理员
	public void delManager(Manager manager) {
		managerMapper.delManager(manager);
		
	}
//查询管理员
	public Manager queryManagerByName(Manager manager) {
		// TODO Auto-generated method stub
		return managerMapper.queryManagerByName(manager.getMname());
	}

	public void updateManager(Manager manager) {
		managerMapper.updateManager(manager);
		
	}

	public EasyUIResult queryAllManagerByPage(Integer page, Integer rows) {
		EasyUIResult result=new EasyUIResult();
		Integer total=managerMapper.queryTotal();
		Integer start=(page-1)*rows;
		List<Manager> MList=managerMapper.queryByPage(start,rows);
		result.setTotal(total);
		result.setRows(MList);
		return result;
	}
}
